﻿/*
 * Cratos Server - WCF Service
 * Cratos Dual WCF Service class
 * 
 * Copyright (c) 2011-2012 Draco
 * 
 * This file is part of Cratos.
 * 
 * Cratos is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Cratos is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cratos.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *       .==.        .==.
 *      //'^\\      //^'\\
 *     // ^ ^\(\__/)/^ ^^\\
 *    //^ ^^ ^/6  6\ ^^ ^ \\
 *   //^ ^^ ^/( .. )\^ ^ ^ \\
 *  // ^^ ^/\| V""V|/\^ ^ ^ \\
 * //^^/\/ /  '~~'  \ \/\^ ^ \\
 *  ------------------------------------
 *  HERE BE DRAGONS
 *  
 *  Johan Dercksen - johan@draco.co.za
 */
using System;
using System.Text;
using System.Linq;
using System.Threading;
using System.ServiceModel;
using System.Configuration;
using System.Collections.Generic;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

using Cratos.Tools.General;
using Cratos.Tools.DataObject;

namespace Cratos.Server.WcfService
{
	/// <summary>
	/// Cratos Dual WCF Service class
	/// </summary>
	public class CratosDualWcfService : ICratosDualWcfService
	{
		#region Class Constant declarations
		#endregion

		#region Class Event declarations
		/// <summary>
		/// Log Message event
		/// </summary>
		public static event CratosLogMessageEventHandler LogMessage;
		#endregion

		#region Class Variable declarations
		#endregion

		#region Class Constructors & Destructors
		/// <summary>
		/// Class Static Constructor
		/// </summary>
		static CratosDualWcfService()
		{
			// Declare the required variables
			UnityConfigurationSection configSection;

			// Create and Setup the Unity Container
			CratosDualWcfService.Container = new UnityContainer();

			// Setup the relevant Configuration
			configSection = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
			if (configSection != null) { configSection.Configure(CratosDualWcfService.Container); }

			// Update the DO Container
			DOCratosGlobal.UnityContainer = CratosDualWcfService.Container;
		}
		#endregion

		#region Class Properties
		/// <summary>
		/// Unity Container
		/// </summary>
		public static IUnityContainer Container { get; set; }
		#endregion

		#region Class Methods
		/// <summary>
		/// Ping the Cratos WCF Service
		/// </summary>
		/// <returns></returns>
		public bool Ping()
		{
			// Log the relevant message
			CratosDualWcfService.OnLogMessage(CratosLogSeverity.Informational, "Cratos Dual WCF Service", "Ping()");

			// Return to calling method
			return (true);
		}

		/// <summary>
		/// Cratos Log Message Event handler
		/// </summary>
		/// <param name="msgSeverity">Message Severity</param>
		/// <param name="msgSource">Message Source</param>
		/// <param name="newMessage">Message</param>
		public static void OnLogMessage(CratosLogSeverity msgSeverity, string msgSource, string newMessage)
		{
			// Declare the required variables

			// Raise the relevant event if required
			if (CratosDualWcfService.LogMessage != null) { CratosDualWcfService.LogMessage(msgSeverity, msgSource, newMessage); }

			// Return to calling method
			return;
		}

		/// <summary>
		/// Cratos Perform Action
		/// </summary>
		/// <param name="ctCratos">Cratos WCF Contract</param>
		/// <returns>A boolean indicating the success of the method</returns>
		public bool CratosPerformAction(CratosWcfContract ctCratos)
		{
			// Declare the required variables
			bool													returnValue = false;
			Thread												engineThread;
			CratosWcfPagingEngine					wcfPagingEngine;
			ICratosDualWcfServiceCallback callbackContext;

			try
			{
				// Log the relevant message
				CratosDualWcfService.OnLogMessage(CratosLogSeverity.Informational, "Cratos Dual WCF Service", 
																					string.Format("CratosPagingCollection() - {0}", Type.GetType(ctCratos.DODataType).Name));

				// Retrieve the Callback Context
				callbackContext = OperationContext.Current.GetCallbackChannel<ICratosDualWcfServiceCallback>();

				// Create and Setup the Collection Engine
				wcfPagingEngine = new CratosWcfPagingEngine(ctCratos.DODataType, callbackContext, ctCratos);
				
				// Subscribe to the relevant event(s)
				wcfPagingEngine.LogMessage += new CratosLogMessageEventHandler(CratosDualWcfService.OnLogMessage);

				// Create, Setup and Start the Thread Processing
				engineThread              = new Thread(new ThreadStart(wcfPagingEngine.EngineProcessing));
				engineThread.IsBackground = true;
				engineThread.Start();

				// Set the successful return value
				returnValue = true;
			}
			catch (Exception runtimeException)
			{
				// Log the exception
				CratosDualWcfService.OnLogMessage(CratosLogSeverity.Exception, "CratosDualWcfService", runtimeException.ToString());

				// Rethrow the exception
				throw;
			}

			// Return to calling method
			return(returnValue);
		}
		#endregion

		#region Class Support Methods
		#endregion
	}
}
